package com.monitorStudy.config;

import com.monitorStudy.controller.CounterDemo;
import com.monitorStudy.controller.GaugeDemo;
import com.monitorStudy.controller.HistogramDemo;
import com.monitorStudy.controller.SummaryDemo;
import io.prometheus.client.Counter;
import io.prometheus.client.Gauge;
import io.prometheus.client.exporter.PushGateway;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.IOException;

/**
 * @author: Alan
 * @date: 2021/5/13 23:19
 */
@Component
public class PrometheusConfig {

    @Autowired
    AppConfig appConfig;

    public static PushGateway prometheusPush;

    public static final Counter counterDemo = Counter.build()
            .name("push_way_counter")
            .labelNames("wy","zxjr","ocs","xxjf","unit","instance")
            .help("Counter 实例")
            .register();

    /**
     * 测试发送
     * @param args
     */
    public static void main(String[] args) {
        prometheusPush = new PushGateway("localhost:9091");
        try {
            for(int i=0;i<50;i++){
                counterDemo.labels("网元","在线接入","OCS","消息计费","byte","localhsot:9091").inc();
                prometheusPush.push(counterDemo,"sp-getway");
                Thread.sleep(5000);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostConstruct
    void init(){
        prometheusPush = new PushGateway(appConfig.getPushgateway());
    }

    @Scheduled(cron="0/5 * * * * ?")
    public void pushGateWay(){
        try {
            counterDemo.labels("网元","在线接入","OCS","消息计费","byte","localhsot:9091").inc();
            counterDemo.labels("网元","在线接入","OCS","消息计费","byte","localhsot:9092").inc();
            prometheusPush.push(counterDemo,"counter1_job");
            prometheusPush.push(CounterDemo.counterDemo,"counter_job");
            prometheusPush.push(GaugeDemo.gaugeDemo,"gauge_job");
            prometheusPush.push(HistogramDemo.histogramDemo,"histogram_job");
            prometheusPush.push(SummaryDemo.summaryDemo,"sumary_job");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
